package cc.fs.kotlinproject.utlis

import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlin.properties.Delegates

/**
 * Created by fostion on 2015/8/15.
 */

public class AlertDialogHelper3 : DialogFragment() {

    var title: String? = null
    var content: String? = null
    var sureText: String? = null
    var contentView: View? = null
    var sureOnclick: DialogInterface.OnClickListener? = null
    var cancleText: String? = null
    var cancleOnclick: DialogInterface.OnClickListener? = null

    public var AlertDialogHelper3.cancelable: Boolean
        get() = isCancelable()
        set(value) = setCancelable(value)

    public fun sureClicke(sure: String, callable: (DialogInterface, Int) -> Unit?) {
        sureText = sure
        sureOnclick = object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface, which: Int) {
                callable?.invoke(dialog, which)
            }
        }
    }

        public fun cancleClick(cancle: String, callable: (DialogInterface, Int) -> Unit?) {
            cancleText = cancle
            cancleOnclick = object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface, which: Int) {
                    callable?.invoke(dialog, which)
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
//        setRetainInstance(true)
        super.onCreate(savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return AlertDialog.Builder(getActivity())
                .setTitle("123")
                .setMessage("456")
                .setView(contentView)
                .setPositiveButton(sureText, sureOnclick)
                .setNegativeButton(cancleText,cancleOnclick)
                .create()
    }
}

@suppress("NOTHING_TO_INLINE")
public inline fun Activity.dialogFragment(context: Context): AlertDialogHelper3 = dialogFragment(context:Context, {})

public inline fun Activity.dialogFragment(context: Context, inlineOptions(InlineOption.ONLY_LOCAL_RETURN) init: AlertDialogHelper3.() -> Unit): AlertDialogHelper3 = ({
    val helper = AlertDialogHelper3()
    helper.init()
    helper
}).invoke()

fun AlertDialog.Builder.sure(content: Any, onClick: DialogInterface.OnClickListener): AlertDialog.Builder {
    return setPositiveButton(content as String, onClick)
}

fun AlertDialog.Builder.cancle(content: Any, onClick: DialogInterface.OnClickListener): AlertDialog.Builder {
    return setNegativeButton(content as String, onClick)
}

fun AlertDialog.Builder.title(title: Any): AlertDialog.Builder {
    when (title) {
        is String -> return setTitle(title)
        else -> return this
    }
}

fun AlertDialog.Builder.message(content: Any): AlertDialog.Builder {
    when (content) {
        is String -> return setMessage(content)
        else -> return this
    }
}





